package com.csu.soa_library.service.Impl;

import com.csu.soa_library.dao.AccountRepository;
import com.csu.soa_library.dao.UserRepository;
import com.csu.soa_library.entity.Account;
import com.csu.soa_library.entity.User;
import com.csu.soa_library.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.login.AccountNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private AccountRepository accountRepository;

    /**
     * 注册
     */
    @Override
    public String registerAccount(Account account) {
        // 检查用户名是否已存在
        if (accountRepository.findByUsername(account.getUsername()).isPresent()) {
            return "用户名已存在！";
        }

        // 创建 User 实体
        User user = new User();
        user.setUsername(account.getUsername());
        user.setPassword(account.getPassword());

        // 保存 Account 和 User
        try {
            accountRepository.save(account);
            userRepository.save(user);
            return "success";
        } catch (Exception e) {
            // 处理保存异常
            return "注册失败，请稍后重试！";
        }
    }
    /**
     * 登录验证
     */
    public Map<String, Object> logAccount(String username, String password) {
        Map<String, Object> response = new HashMap<>();

        // 根据用户名精确查询用户
        Optional<User> optionalUser = userRepository.findByUsername(username);

        if (!optionalUser.isPresent()) {
            response.put("message", "用户名错误！");
            response.put("userId", null);
            return response;
        }

        User user = optionalUser.get();

        // 比较输入的密码和数据库中的密码
        if (user.getPassword().equals(password)) {
            response.put("message", "success");
            response.put("userId", user.getUserId());
        } else {
            response.put("message", "密码错误！");
            response.put("userId", null);
        }

        return response;
    }
    /**
     * 获取用户信息
     */
    @Override
    public Account getAccountInfo(int accountID) {
        Optional<Account> account = accountRepository.findById(accountID);
        if (account.isPresent()) {
            Account account1 =account.get();
            return account1;
        } else {
            return null;
        }
    }
    /**
     * 更新用户信息
     */
    @Override
    public String updateAccountInfo(Account account) {
        // 首先检查要更新的账户是否存在
        Optional<Account> existingAccount = accountRepository.findById(account.getAccountId());

        // 如果账户不存在，返回错误信息
        if (!existingAccount.isPresent()) {
            return "账户不存在，无法更新！";
        }

        try {
            // 保存更新后的账户信息
            accountRepository.save(account);
            return "success";
        } catch (Exception e) {
            // 捕获可能的异常并返回错误信息
            return "更新账户信息失败，请稍后再试！";
        }
    }

    @Override
    public String deleteAccount(int accountID) {
        Optional<Account> existingAccount = accountRepository.findById(accountID);
        // 如果账户不存在，返回错误信息
        if (!existingAccount.isPresent()) {
            return "账户不存在，无法删除！";
        }
        Account account =accountRepository.getReferenceById(accountID);
        Optional<User> optionalUser = userRepository.findByUsername(account.getUsername());
        User user = optionalUser.get();
        try {
            accountRepository.delete(account);
            userRepository.delete(user);
            return "success";
        } catch (Exception e) {
            // 捕获可能的异常并返回错误信息
            return "更新账户信息失败，请稍后再试！";
        }
    }
}
